﻿
using PoemGame.Domain.GameAggregate;

namespace PoemGame.Domain.Services.Factory
{
    public class DomainServiceFactory<T> : IDomainServiceFactory<T> where T : IDomainService
    {
        private readonly IServiceProvider serviceProvider;
        public DomainServiceFactory(IServiceProvider _serviceProvider)
        {
            serviceProvider = _serviceProvider;
        }
        public T GetService(GameType gamePlayType)
        {
            var name = typeof(T).Name.TrimStart('I');

            var assname = "PoemGame.Domain.Services." + gamePlayType.MainType;
            if (!string.IsNullOrEmpty(gamePlayType.SubType))
            {
                assname += "_" + gamePlayType.SubType;
            }
            var typename = "PoemGame.Domain.Services." + gamePlayType.MainType + "." + gamePlayType.MainType + name;
            if (!string.IsNullOrEmpty(gamePlayType.SubType))
            {
                typename = "PoemGame.Domain.Services." + gamePlayType.MainType + "_" + gamePlayType.SubType + "." + gamePlayType.MainType + "_" + gamePlayType.SubType + name;
            }

            Type type = Type.GetType(typename + "," + assname);

            return (T)serviceProvider.GetService(type);
        }


    }
}